﻿// LoginDlg.cpp: 实现文件
//

#include "pch.h"
#include "ProjectAA.h"
#include "LoginDlg.h"
#include "afxdialogex.h"


// CLoginDlg 对话框

IMPLEMENT_DYNAMIC(CLoginDlg, CDialogEx)

CLoginDlg::CLoginDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_DIALOG_LOGIN, pParent)
	, User(_T(""))
	, Pallent(_T(""))
	, Program(_T(""))
	, FryTime(0)
	, m_cPassward(_T(""))
	, m_cNewPassword(_T(""))
{

}

CLoginDlg::~CLoginDlg()
{
}

void CLoginDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_EDIT1, User);
	DDX_Text(pDX, IDC_EDIT2, Pallent);
	DDX_Text(pDX, IDC_EDIT3, Program);
	DDX_Text(pDX, IDC_EDIT4, FryTime);
	DDX_Text(pDX, IDC_EDIT_USERNAME, m_cUserName);
	DDX_Text(pDX, IDC_EDIT_PASSWORD, m_cPassward);
}


BEGIN_MESSAGE_MAP(CLoginDlg, CDialogEx)
	ON_BN_CLICKED(IDC_BUTTON1, &CLoginDlg::OnBnClickedButton1)
	ON_BN_CLICKED(IDC_BUTTON2, &CLoginDlg::OnBnClickedButton2)
	ON_BN_CLICKED(IDC_BUTTON3, &CLoginDlg::OnBnClickedButton3)
END_MESSAGE_MAP()


// CLoginDlg 消息处理程序


void CLoginDlg::OnBnClickedButton1()
{
	SaveValueToConfig();
}


BOOL CLoginDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	CComboBox* combo_box = nullptr;
	combo_box = (CComboBox*)GetDlgItem(IDC_COMBO_MODE_CHOOSE);

	combo_box->InsertString(0, L"员工");
	combo_box->InsertString(1, L"管理员");
	combo_box->SetCurSel(0);

	m_cPasswordCorrect = gSystemMgr.m_sLoginInfo.m_cPassword;
	m_cUserNameCorrect = gSystemMgr.m_sLoginInfo.m_cUserName;

	ReadConfigIni();

	return TRUE;
}


void CLoginDlg::OnCancel()
{
	// TODO: 在此添加专用代码和/或调用基类

	CDialogEx::OnCancel();
}

void CLoginDlg::ReadConfigIni()
{
	User = gSystemMgr.UserMsg;
	Pallent = gSystemMgr.PallentMsg;
	Program = gSystemMgr.ProgramMsg;
	FryTime = gSystemMgr.FryTime;

	UpdateData(FALSE);
}

void CLoginDlg::SaveValueToConfig()
{
	UpdateData(TRUE);

	//gSystemMgr.UserMsg = User;
	gSystemMgr.PallentMsg = Pallent;
	gSystemMgr.ProgramMsg = Program;
	gSystemMgr.FryTime = FryTime;

	CString cstr;

	//gSystemMgr.m_cLoginConfigIni.SaveConfigIni(L"Login", L"UserMsg", gSystemMgr.UserMsg);

	gSystemMgr.m_cLoginConfigIni.SaveConfigIni(L"Login", L"PallentMsg", gSystemMgr.PallentMsg);

	gSystemMgr.m_cLoginConfigIni.SaveConfigIni(L"Login", L"ProgramMsg", gSystemMgr.ProgramMsg);

	cstr.Format(L"%.2f", gSystemMgr.FryTime);
	gSystemMgr.m_cLoginConfigIni.SaveConfigIni(L"Login", L"FryTime", cstr);

	gSystemLog.DisplayLogAndSave(L"Login保存数据完成", LV_EVENT);
}

void CLoginDlg::EnableControl(const BOOL nflag)
{
	CButton* btn = nullptr;
	btn = (CButton*)GetDlgItem(IDC_BUTTON3);
	btn->EnableWindow(nflag);

	CButton* btnLogin = nullptr;
	btnLogin = (CButton*)GetDlgItem(IDC_BUTTON2);
	btnLogin->EnableWindow(!nflag);

	CMenu* pMenu = GetMenu();
	pMenu = AfxGetMainWnd()->GetMenu();

	if (ModeFlag == 1)
	{
		if (nflag == 1)
		{
			pMenu->EnableMenuItem(ID_32787, MF_ENABLED);
			pMenu->EnableMenuItem(ID_32784, MF_ENABLED);
			pMenu->EnableMenuItem(ID_32785, MF_ENABLED);
			pMenu->EnableMenuItem(ID_MENU_SYS, MF_ENABLED);
			pMenu->EnableMenuItem(ID_32784, MF_ENABLED);
			pMenu->EnableMenuItem(ID_32789, MF_ENABLED);
		}
		else
		{
			pMenu->EnableMenuItem(ID_32787, MF_DISABLED);
			pMenu->EnableMenuItem(ID_32784, MF_DISABLED);
			pMenu->EnableMenuItem(ID_32785, MF_DISABLED);
			pMenu->EnableMenuItem(ID_MENU_SYS, MF_DISABLED);
			pMenu->EnableMenuItem(ID_32784, MF_DISABLED);
			pMenu->EnableMenuItem(ID_32789, MF_DISABLED);
		}
	}
}


void CLoginDlg::OnBnClickedButton2()
{
	UpdateData(TRUE);

	CComboBox* combo_box = (CComboBox*)(GetDlgItem(IDC_COMBO_MODE_CHOOSE));
	auto cboSelcect = combo_box->GetCurSel();

	//if(m_cPasswordCorrect == L"")
	//{
	//	m_cPasswordCorrect = L"1";
	//}

	if (cboSelcect == 0)
	{
		if (m_cUserName != L"")
		{
			//int i = _ttoi(m_cUserName);
			gSystemMgr.UserMsg = m_cUserName;
			CString cstr;
			cstr.Format(L"员工号:%s  员工登陆成功!", m_cUserName);
			gSystemLog.DisplayLogAndSave(cstr.GetBuffer(), LV_EVENT);
			OnCancel();

			EnableControl(TRUE);
		}
		else
		{
			gSystemLog.DisplayLogAndSave(L"请输入员工号!", LV_ERROR);
		}

	}
	if (cboSelcect == 1)
	{
		UpdateData(TRUE);

		if (m_cUserName == m_cUserNameCorrect /*!= L""*/ && m_cPassward == m_cPasswordCorrect)
		{
			gSystemMgr.UserMsg = m_cUserName;
			ModeFlag = 1;
			//int i = _ttoi(m_cUserName);
			CString cstr;
			cstr.Format(L"员工号:%s  管理员登陆成功!", m_cUserName);
			gSystemLog.DisplayLogAndSave(cstr.GetBuffer(), LV_EVENT);
			OnCancel();
			EnableControl(TRUE);
		}
		else
		{
			gSystemLog.DisplayLogAndSave(L"登陆失败!", LV_ERROR);
		}

	}

	m_cUserName = L"";
	m_cPassward = L"";
	UpdateData(FALSE);
}


void CLoginDlg::OnBnClickedButton3()
{
	gSystemLog.DisplayLogAndSave(L"退出登录成功!", LV_EVENT);
	EnableControl(FALSE);
	ModeFlag = 0;
}
